using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml;

namespace converter.template.item
{
    /// <summary>
    /// A base class for a template item. Provides common functionality to all items.
    /// </summary>
    public abstract class TemplateItem
    {
        /// <summary>
        /// Gets a list of child items.
        /// </summary>
        public List<TemplateItem> Items
        {
            get
            {
                return items;
            }
        }

        /// <summary>
        /// Gets a parent of this item. Returns null if the item does not have a parent.
        /// </summary>
        public TemplateItem Parent
        {
            get
            {
                return parent;
            }
        }

        public string ItemName
        {
            get
            {
                return itemName;
            }
            set
            {
                itemName = value;
            }
        }

        /// <summary>
        /// Adds an item to a list of child items. Sets itself as the new item's parent.
        /// </summary>
        /// <param name="item">An item to be added.</param>
        public void Add(TemplateItem item)
        {
            items.Add(item);
            item.parent = this;
        }

        public Style GetStyle()
        {
            return style;
        }

        protected void SetStyle(Style style)
        {
            this.style = style;
        }

        /// <summary>
        /// Base class element builder.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        public virtual void Build(OpenXmlCompositeElement parent)
        {
        }

        private readonly List<TemplateItem> items = new List<TemplateItem>();
        private Style style = new Style();

        /// <summary>
        /// A parent, this items is child of.
        /// </summary>
        TemplateItem parent;

        string itemName = string.Empty;
    }
}
